package com.zl.zlbaseproject.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zl.zlbaseproject.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by 郑磊 on 2018/6/17.
 * 仅维护成功 和 空布局   失败布局用自定义的item代替   loading布局使用smartRefreshLayout展示。
 */

public class LayoutManager extends FrameLayout {
    int mEmptyImage;
    CharSequence mEmptyText;

    ZlLayoutManager.OnInflateListener mOnEmptyInflateListener;

    int mTextColor;
    int mTextSize;
    int mEmptyResId;

    int mContentId;
    Map<Integer, View> mLayouts;
    LayoutInflater mInflater;


    public LayoutManager(@NonNull Context context) {
        this(context, null,0 );
    }

    public LayoutManager(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LayoutManager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mEmptyResId = -1;

        this.mContentId = -1;
        this.mLayouts = new HashMap();
        this.mInflater = LayoutInflater.from(context);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZlLayoutManager);
//        this.mEmptyImage = a.getResourceId(R.styleable.ZlLayoutManager_zlEmptyImage, R.mipmap.icon_me);
//        this.mEmptyText = a.getString(R.styleable.ZlLayoutManager_zlEmptyText);
//        this.mErrorText = a.getString(R.styleable.ZlLayoutManager_zlErrorText);
//        this.mRetryText = a.getString(R.styleable.ZlLayoutManager_zlRetryText);
//        this.mTextColor = a.getColor(R.styleable.LoadingLayout_llTextColor, -6710887);
//        this.mTextSize = a.getDimensionPixelSize(R.styleable.LoadingLayout_llTextSize, this.dp2px(16.0F));
//        this.mButtonTextColor = a.getColor(R.styleable.LoadingLayout_llButtonTextColor, -6710887);
//        this.mButtonTextSize = a.getDimensionPixelSize(R.styleable.LoadingLayout_llButtonTextSize, this.dp2px(16.0F));
//        this.mButtonBackground = a.getDrawable(R.styleable.LoadingLayout_llButtonBackground);
        this.mEmptyResId = a.getResourceId(R.styleable.ZlLayoutManager_zlEmptyResId, R.layout.default_empty);

        a.recycle();
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        if(this.getChildCount() != 0) {
            if(this.getChildCount() > 1) {
                this.removeViews(1, this.getChildCount() - 1);
            }
            View view = this.getChildAt(0);
            this.setContentView(view);
        }
    }


    public void setContentView(View view) {
        this.mContentId = view.getId();
        this.mLayouts.put(Integer.valueOf(this.mContentId), view);
    }


    public LayoutManager setEmpty(@LayoutRes int id) {
        if(this.mEmptyResId != id) {
            this.remove(this.mEmptyResId);
            this.mEmptyResId = id;
        }
        return this;
    }

    public LayoutManager setOnEmptyInflateListener(ZlLayoutManager.OnInflateListener listener) {
        this.mOnEmptyInflateListener = listener;
        if(this.mOnEmptyInflateListener != null && this.mLayouts.containsKey(Integer.valueOf(this.mEmptyResId))) {
            listener.onInflate((View)this.mLayouts.get(Integer.valueOf(this.mEmptyResId)));
        }

        return this;
    }

//    public LayoutManager setOnErrorInflateListener(ZlLayoutManager.OnInflateListener listener) {
//        this.mOnErrorInflateListener = listener;
//        if(this.mOnErrorInflateListener != null && this.mLayouts.containsKey(Integer.valueOf(this.mErrorResId))) {
//            listener.onInflate((View)this.mLayouts.get(Integer.valueOf(this.mErrorResId)));
//        }
//
//        return this;
//    }

    public LayoutManager setEmptyImage(@DrawableRes int resId) {
        this.mEmptyImage = resId;
        this.image(this.mEmptyResId, R.id.empty_image, this.mEmptyImage);
        return this;
    }

    public LayoutManager setEmptyText(String value) {
        this.mEmptyText = value;
        this.text(this.mEmptyResId, R.id.empty_text, this.mEmptyText);
        return this;
    }



//    public LayoutManager setRetryListener(OnClickListener listener) {
//        this.mRetryListener = listener;
//        return this;
//    }



    public void showEmpty() {
        this.show(this.mEmptyResId);
    }


    public void showContent() {
        this.show(this.mContentId);
    }

    private void show(int layoutId) {
        Iterator iterator = this.mLayouts.values().iterator();

        while(iterator.hasNext()) {
            View view = (View)iterator.next();
            view.setVisibility(View.GONE);
        }

        this.layout(layoutId).setVisibility(View.VISIBLE);
    }

    private void remove(int layoutId) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            View vg = (View)this.mLayouts.remove(Integer.valueOf(layoutId));
            this.removeView(vg);
        }

    }

    private View layout(int layoutId) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            return (View)this.mLayouts.get(Integer.valueOf(layoutId));
        } else {
            View layout = this.mInflater.inflate(layoutId, this, false);
            layout.setVisibility(View.GONE);
            this.addView(layout);
            this.mLayouts.put(Integer.valueOf(layoutId), layout);
            ImageView img;
            TextView txt;
            if(layoutId == this.mEmptyResId) {
                img = (ImageView)layout.findViewById(R.id.empty_image);
                if(img != null) {
                    img.setImageResource(this.mEmptyImage);
                }

                txt = (TextView)layout.findViewById(R.id.empty_text);
                if(txt != null) {
                    txt.setText(this.mEmptyText);
                    txt.setTextColor(this.mTextColor);
                    txt.setTextSize(0, (float)this.mTextSize);
                }

                if(this.mOnEmptyInflateListener != null) {
                    this.mOnEmptyInflateListener.onInflate(layout);
                }
            }
            return layout;
        }
    }

    private void text(int layoutId, int ctrlId, CharSequence value) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            TextView view = (TextView)((View)this.mLayouts.get(Integer.valueOf(layoutId))).findViewById(ctrlId);
            if(view != null) {
                view.setText(value);
            }
        }

    }

    private void image(int layoutId, int ctrlId, int resId) {
        if(this.mLayouts.containsKey(Integer.valueOf(layoutId))) {
            ImageView view = (ImageView)((View)this.mLayouts.get(Integer.valueOf(layoutId))).findViewById(ctrlId);
            if(view != null) {
                view.setImageResource(resId);
            }
        }

    }

    public interface OnInflateListener {
        void onInflate(View var1);
    }
}
